<template>
  <h4>和是：{{ sum }}</h4>
  <button @click="sum++">点我和加一</button>
  <hr />
  <h2>原姓名：{{ name }}</h2>
  <h2>姓名：{{ name }}</h2>
  <h2>年龄：{{ age }}</h2>
  <h2>薪水:{{ job.j1.salary }}K</h2>
  <h3 v-if="person.car">汽车:{{ person.car }}</h3>

  <button @click="name += '!'">改名</button>
  <button @click="age++">增寿</button>
  <button @click="job.j1.salary++">加薪</button>
  <hr />
  <button @click="showRawPerson">输出最原始的person</button>
  <button @click="addCar">给人添加一台车</button>
  <button @click="changeCar">换车</button>
</template>

<script>
import { reactive, ref, toRefs, toRaw, markRaw } from "vue";
export default {
  name: "Demo",
  beforeCreate() {},

  setup(props, context) {
    let sum = ref(0);
    let person = reactive({
      name: "张三",
      age: 18,
      job: {
        j1: {
          salary: 20,
        },
      },
    });

    function showRawPerson() {
      const p = toRaw(person);
      p.age++;
      console.log(p);
    }

    function addCar() {
      let car = {
        name: "仰望",
        price: "40W",
      };
      //person.car = car;
      person.car = markRaw(car);
    }

    function changeCar(){
      person.car.name = '问界';
      console.log(person.car);
    }

    return {
      sum,
      person,
      ...toRefs(person),
      showRawPerson,
      addCar,
      changeCar,
    };
  },
};
</script>